package com.agileai.bpm.interceptor;

import com.agileai.bpm.common.VariableInterpreter;
import com.agileai.bpm.domain.running.ActivityInstance;
import com.agileai.bpm.domain.running.ProcessVariableInstance;
import com.agileai.bpm.model.InterceptorModelDocument.InterceptorModel;

public abstract class BaseInterceptor {
	protected String type = null;
	protected String name = null;
	protected String position = null;
	
	protected boolean isSynchronous = false;
	protected String variableCode = null;
	protected String varExpression = null;
	
	abstract public void doIntercept(ActivityInstance activityInstance) throws Exception;
	abstract public void handleException(ActivityInstance activityInstance,Exception ex);

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public static final class Type{
		public static final String DBOperater = "DBOperater";
		public static final String RestInvoker = "RestInvoker";
		public static final String FlowLauncher = "FlowLauncher";
		public static final String WatchInvoker = "WatchInvoker";
	}
	public static final class Position{
		public static final String in = "in";
		public static final String out = "out";
	}
	public static String getInterceptorType(InterceptorModel interceptorModel){
		String result = null;
		if (interceptorModel.getDBOperater() != null){
			result = BaseInterceptor.Type.DBOperater;
		}
		else if (interceptorModel.getRestInvoker() != null){
			result = BaseInterceptor.Type.RestInvoker;
		}
		else if (interceptorModel.getFlowLauncher() != null){
			result = BaseInterceptor.Type.FlowLauncher;
		}
		else if (interceptorModel.getWatchInvoker() != null){
			result = BaseInterceptor.Type.WatchInvoker;
		}
		return result;
	}
	public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	public boolean isSynchronous() {
		return isSynchronous;
	}
	public void setSynchronous(boolean isSynchronous) {
		this.isSynchronous = isSynchronous;
	}
	public String getVariableCode() {
		return variableCode;
	}
	public void setVariableCode(String variableCode) {
		this.variableCode = variableCode;
	}
	protected String replaceSimpleVariable(String template,ProcessVariableInstance procVariableInstance){
		return VariableInterpreter.replaceSimpleVariable(template, procVariableInstance);
	}
	protected String replaceMapVariable(String template,ProcessVariableInstance procVariableInstance){
		return VariableInterpreter.replaceMapVariable(template, procVariableInstance);
	}
}
